package com.qf.web.school_major.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.utils.ResultUtil;
import com.qf.utils.ResultVo;
import com.qf.web.school_college.entity.SchoolCollege;
import com.qf.web.school_college.service.SchoolCollegeService;
import com.qf.web.school_major.entity.MajorList;
import com.qf.web.school_major.entity.SchoolMajor;
import com.qf.web.school_major.service.SchoolMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xufuhan
 * @Date 2024/9/12 14:04
 */
@RestController
@RequestMapping("/api/major")
public class SchoolMajorController {
    @Autowired
    private SchoolMajorService schoolMajorService;
    @Autowired
    private SchoolCollegeService schoolCollegeService;

    @PostMapping
    public ResultVo add(@RequestBody SchoolMajor schoolMajor) {
        boolean save = schoolMajorService.save(schoolMajor);
        if (save) {
            return ResultUtil.success("新增专业成功");
        } else {
            return ResultUtil.error("新增专业失败");
        }
    }

    @PutMapping
    public ResultVo edit(@RequestBody SchoolMajor schoolMajor) {
        boolean save = schoolMajorService.updateById(schoolMajor);
        if (save) {
            return ResultUtil.success("编辑专业成功");
        } else {
            return ResultUtil.error("编辑专业失败");
        }
    }

    @DeleteMapping("/{majorId}")
    public ResultVo delete(@PathVariable("majorId") Long majorId) {
        boolean remove = schoolMajorService.removeById(majorId);
        if (remove) {
            return ResultUtil.success("删除专业成功");
        } else {
            return ResultUtil.error("删除专业失败");
        }
    }
    //专业列表
    @GetMapping("/api/list")
    public ResultVo getList(MajorList list) {
        IPage<SchoolMajor> list1 = schoolMajorService.getList(list);
        return ResultUtil.success("查询成功",list1);
    }

    //学院列表
    @GetMapping("/getCollegeList")
    public ResultVo getCollegeList() {
        List<SchoolCollege> list1 = schoolCollegeService.list();
        return ResultUtil.success("查询成功",list1);
    }
}
